%Script used for processing LISST data that has been collected using the CHSD Profiler.
%
%This program computes loops through the ascii files containing software
%processed LISST data, with file names listed below. This script loops
%through the functions listed above, and outputs a mat file containing the
%burst average LISST data for each station (called filename.mat). 
%
%
%
%Calls the following functions or scripts (Be sure they are in your matlab path or in the folder you are running this in):
%       
%   1. LISST100Xasc2mat_v2.m: Converts .asc files to .mat files, calculates area concentration distirubiton, 
%      and calculates additional particle size parameters. Calls function datanumfromdata.m and compute_mean_v2.m. 
%      See function for more information. 
%               
%    2.getscat_kaf.m:Function to get the scattering, transmission and zscat from a binary. We use it to estimate scattering (b),
%                absorption (indirectly), and to double check beam attenuation through another approximation of tau.
%                REQUIRED FILES: ringarea.asc file, zscat.asc file and tt2mat.m
%                The OUTPUTS are:
%                     tau_2: the optical transmission
%                     c_2: beam attenuation [m^-1]
%                     b:Near forward scattering coefficient [m^-1]  
%                     a:Calculated absorption coefficiet [m^-1]
%
%                    
%    3. getLISST100X_AVGdata_fromprofiler_v2.m: Computes burst avearges 
%
%    4.create_data_struct_csv_profiler_LISST100X.m: Compiles all samples
%    from 1 cruise and saves as a .csv file and matlab strucutre
%
% Based off GetLISST100Xdata_CTDpress.m Orginially written by G. Massey 
%
%KAF November 2016 

%% replace the files in "filename" to reflect the stations you are processing
clear
cruise='YR160907';

LISSTdir='/Users/kelsey/Desktop/Dissertation/Profiler Surveys/York River/Axial Surveys/YR160907/LISST/Redo rs/';

PRM=80; %Path Length Reduction Module used, either 0 (non), 50 (50%), 80 (80%), or 90 (90%)
year=2016; %Year of cruise
zscfile=fullfile(LISSTdir,'zsc160818_1239_80percent.asc');

%Enter LISST filenemas
LISSTname=cellstr(['L5720'
'L5721'
'L5722'
'L5723'
'L5724'
'L5725'
]);

%Enter Distance from the York River Mouth (or reference point of your
%choice).

distance=[11
    18
    30
    39
    45
    60];


iRAW=true; %flag for raw  (*.log files). True if *.log files available and scatter can be created. 
%iRAW=false;


correctVCC=false;
InvType=31;


%%

load('size100x.asc'); %asc file containing the median bin size for each of the 32 bins

%for ii=1:length(LISSTname)
for ii=1:length(LISSTname)  
    if InvType==31 || InvType==21
        [vd,vd_A,vars]=LISST100Xasc2mat_v2([char(LISSTname(ii)),'_rs'],PRM,year,correctVCC,InvType,LISSTdir);
    else
        [vd,vd_A,vars]=LISST100Xasc2mat_v2(char(LISSTname(ii)),PRM,year,correctVCC,InvType,LISSTdir);
    end
%Calculate near forward scattering and absportion (indirectly) using getscat_kaf.m. 
%MUST HAVE RAW DATA FILE (.log) to use. If no .log file be iRAW=false. 

%Set X=1 for LISST-100X data
if iRAW
HK3Scale=0.0008738;%Laser Reference Scale,mW (0 offset) from From LISST.ini (Instrument specific)  
[~,tau_2,c_2,b,a,~] = getscat_kaf_1239([char(LISSTname(ii)) '.log'],zscfile,1,fullfile(LISSTdir,'ringarea_1239.asc'),HK3Scale,PRM);
%[data,tau_2,c_2,b,a,cscat] = getscat_kaf_1239(datafile,zscfile,X,ringareafile,HK3Scale,PRM)
%[tau_2,c_2,b,a] = getscat_kaf_1239([char(LISSTname(ii)) '.log'],fullfile(LISSTdir,'zsc160602_1239.asc'),1,fullfile(LISSTdir,'ringarea_1239.asc'));
Ldata=getLISST100X_burstAVGdata_fromprofiler_noCTD(LISSTname(ii),LISSTdir,vars,vd,vd_A,b,a,c_2,tau_2);  
else
tau_2=NaN(length(vars));
c_2=NaN(length(vars));
b=NaN(length(vars));
a=NaN(length(vars));
Ldata=getLISST100X_burstAVGdata_fromprofiler_noCTD(LISSTname(ii),vars,vd,vd_A,b,a,c_2,tau_2);  
end

clear tau_2 c_2 b a vd vd_A vars
end

% LdataC=Ldata; %corrected for wrong calibration constants and software version
% LdataC(:,2:33)=Ldata(:,4:35)./0.85;
% LdataC(:,37:39)=Ldata(:,37:39)./0.85;
% LdataC(:,43:75)=Ldata(:,43:75)./0.85;

%% Compiles all burst average LISST data from one curise into one matrix where each row corresponds to a different sample/depth, and
% create and save compiled data into .csv file and as a  matlab strucutre. 

Ldata=create_data_struct_csv_profiler_LISST100X_v2([cruise,'_v2'],LISSTdir,size100x,distance,LISSTname);
    
save(fullfile(LISSTdir,[cruise,'_v2']),'-struct','Ldata'); 

